#include "stdafx.h"
#include ".\screenraypicker.h"
#include "windows.h"

ScreenRayPicker::ScreenRayPicker(void)
{
	m_pView = NULL;
	m_lastVecValid = false;
}

ScreenRayPicker::~ScreenRayPicker(void)
{
}

bool ScreenRayPicker::PickRayFromScreenCoord(const mdsVector<2, double> & p2ScreenCoord) 
{
	
	if(!m_pView)
	{
		m_lastVecValid = false;
		return false;
	}
	m_lastVecValid = true;

	m_rayPos = m_pView->GetPosition();

	//double m_lfAspect, m_lfAngle, m_lfTanAngle, m_lfNearClip;
	//const viewInfo * m_pView;
	
	// look=0, left=1, up=2
	m_lastVecPicked[0] = 1.0;
	m_lastVecPicked[1] = m_lfAspect*m_lfTanAngle*p2ScreenCoord.x();
	m_lastVecPicked[2] = m_lfTanAngle * p2ScreenCoord.y();

	m_lastVecPicked = m_pView->XformVecRotOnly(m_lastVecPicked);

	m_rayPos = m_pView->GetPosition() + m_lastVecPicked*m_lfNearClip;

	return true;
}

bool ScreenRayPicker::PickRayFromScreenCoordStateless(const mdsVector<2, double> & p2ScreenCoord,
									 mdsRay<double> & rayOut) const
{
	if(!m_pView)
	{
		TRACE("FALSE!\n");
		return false;
	}

	//double m_lfAspect, m_lfAngle, m_lfTanAngle, m_lfNearClip;
	//const viewInfo * m_pView;
	
	// look=0, left=1, up=2
	rayOut.m_p3Dir[0] = 1.0;
	rayOut.m_p3Dir[1] = m_lfAspect*m_lfTanAngle*p2ScreenCoord.x();
	rayOut.m_p3Dir[2] = m_lfTanAngle * p2ScreenCoord.y();

	if(m_pView->GetViewBack() > 0.0)
	{
		rayOut.m_p3Dir *= -1.0;
	}

	rayOut.m_p3Dir = m_pView->XformVecRotOnly(rayOut.m_p3Dir);

	rayOut.m_p3Pos = m_pView->GetPosition() + rayOut.m_p3Dir*m_lfNearClip;

	return true;
}